package com.bestv.database.data;

import com.bestv.database.vo.ProgramType;
import com.bestv.utils.database.DBConnectionManager;
import com.bestv.utils.database.JDBCHelper;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by Henry on 14-7-4.
 */
public class BestvDataManager {
    private ConcurrentHashMap<String, ProgramType> programTypes = null;

    private static BestvDataManager instance;

    public static synchronized BestvDataManager getInstance() {
        if (instance == null) {
            instance = new BestvDataManager();
        }
        return instance;
    }

    private BestvDataManager() {
        getProgramTypesFromDB();
    }

    private void getProgramTypesFromDB() {
        String sql = String.format("SELECT * FROM %s", "programtype");
        Connection conn = DBConnectionManager.getSlaveConnection();
        ArrayList<Object> rs = JDBCHelper.executeQuery(conn, sql, ProgramType.class);
        JDBCHelper.close(conn);

        programTypes = new ConcurrentHashMap<String, ProgramType>();
        for (Object object : rs) {
            ProgramType type = (ProgramType) object;
            programTypes.put(type.getAlias(), type);
        }
    }

    public ProgramType getTypeFromAlias(String alias) {
        return programTypes.get(alias);
    }
}
